package com.birdboot.controller;

import com.birdboot.annotation.Controller;
import com.birdboot.annotation.RequestMapping;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Controller
public class ArticleController {
    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        try {
            Connection connection = DbUtil.getConnection();
            String sql = "INSERT INTO article(title,author,content) VALUES (?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, title);
            ps.setString(2, author);
            ps.setString(3, content);
            int i = ps.executeUpdate();

            if(i > 0){
                //发表成功
                response.sendRedirect("/article_success.html");
            }else{
                //发表失败
                response.sendRedirect("/article_fail.html");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

}
